<?php
session_start();
	class block_twitter_read extends block_base
	{
	   
	    function init()
	    {
	        $this->title = "Twitter Feed";
	        $this->version = 2008112700;
	    }
	    
	    function specialization()
	    {
    		// Update the title appropriately
    		$titlestring = "Twitter Feed";
    		
    		$this->title = $titlestring;
		}
	    
	    function get_content()
	    {	    	
	    	if ($this->content !== NULL)
	    	{
	        	return $this->content;
	    	}
	
	    	$this->content = new stdClass;	    	
			
						
			// display the login box for users to enter their details and preferences
			
			// To get wwwroot we must have access to the $CFG
			global $CFG;
			
			$thispage = "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
			$thisroot = $CFG->wwwroot.$this->config->block_twitter_read_path;
						
			$output = "<html>
				<body>
				<script type=\"text/javascript\">
				function getFeed()
				{
					var xmlHttp;
					
					document.cookie = \"twitterReadUser=\" + escape(document.twitterReadForm.twitterReadUser.value) + \";path=/\";
					document.cookie = \"twitterReadPass=\" + escape(document.twitterReadForm.twitterReadPass.value) + \";path=/\";
					document.cookie = \"twitterReadFeed=\" + escape(document.twitterReadForm.twitterReadFeed.value) + \";path=/\";
					document.cookie = \"twitterReadNumber=\" + escape(document.twitterReadForm.twitterReadNumber.value) + \";path=/\";
					document.cookie = \"twitterReadTweme=\" + escape(document.twitterReadForm.twitterReadTweme.value) + \";path=/\";
					
					try
					{
						xmlHttp = new XMLHttpRequest();
					}
					catch(e)
					{
						try
						{
							xmlHttp = new ActiveXObject(\"Msxml2.XMLHTTP\");
						}
						catch(e)
						{
							try
							{
								xmlHttp = new ActiveXObject(\"Microsoft.XMLHTTP\");
							}
							catch(e)
							{
								alert(\"Your browser does not support AJAX!\");
								return false;
							}
						}
					}
					
					xmlHttp.onreadystatechange=function()
					{
						if(xmlHttp.readyState==4)
						{
							document.getElementById(\"feedcontent\").innerHTML = xmlHttp.responseText;
						}
					}
					
					xmlHttp.open(\"GET\", \"". $thisroot ."lib.php\", true);
					xmlHttp.send(null);

				}
				
								
				</script>
							
				<form name=\"twitterReadForm\">
   				Username: <input type=\"text\" name=\"twitterReadUser\"/><br />
    			Password: <input type=\"password\" name=\"twitterReadPass\"/><p />
    			Feed: <select name=\"twitterReadFeed\">
	 				<option>My friends</option>
	 				<option>Public</option>
	 				<option>My own</option>
 				</select><p />
 				Number of results: <select name=\"twitterReadNumber\">
	 				<option>5</option>
	 				<option>10</option>
	 				<option>15</option>
	 				<option>20</option>
 				</select><p />
 				<hr />
 				Tweme to follow: <input type=\"text\" name=\"twitterReadTweme\"/><p />
 				<input type=\"button\" value=\"Update Feed\" onClick=\"getFeed();\" />
    			</form>
    			
    			<hr />
				
				<script type=\"text/javascript\">
				
					document.twitterReadForm.twitterReadUser.value = get_cookie(\"twitterReadUser\");
					document.twitterReadForm.twitterReadPass.value = get_cookie(\"twitterReadPass\");
					document.twitterReadForm.twitterReadFeed.value = get_cookie(\"twitterReadFeed\");
					document.twitterReadForm.twitterReadNumber.value = get_cookie(\"twitterReadNumber\");
					document.twitterReadForm.twitterReadTweme.value = get_cookie(\"twitterReadTweme\");
				
				</script>				
										
    			<span id=\"feedcontent\"></span>
								
				</body>
				</html>";
			
	    	$this->content->footer = "";
	    	$this->content->text = $output;
	    	return $this->content;
		}
		
		
		
		function instance_allow_config()
		{
	    	// Allow user configuration
	    	return true;
		}
		
		function instance_allow_multiple()
		{
    		// Allow multiple instances of the block
    		return true;
		}
		
		function preferred_width() {
    		// The preferred value is in pixels
    		return 200;
		}
			
	}
?>